package cn.com.jslh.boarpro.main.presenter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Toast;

import com.blankj.utilcode.util.SPUtils;

import net.idik.lib.slimadapter.SlimAdapter;
import net.idik.lib.slimadapter.SlimAdapterEx;
import net.idik.lib.slimadapter.SlimInjector;
import net.idik.lib.slimadapter.viewinjector.IViewInjector;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.base.BaseApplication;
import cn.com.jslh.boarpro.base.mvp.BaseListPresenter;
import cn.com.jslh.boarpro.base.mvp.BaseListView;
import cn.com.jslh.boarpro.main.model.BreedingRecordModelImpl;
import cn.com.jslh.boarpro.tables.BreedRecordTable;
import cn.com.jslh.boarpro.util.WebService;
import cn.com.jslh.boarpro.util.database.DataBaseUtil;

/**
 * 作者：周洲 on 2017/7/26 11:01
 * <p>
 * 邮箱：double0zhou@126.com
 */

public class BreedingRecordPresenterImpl extends BaseListPresenter {
//    private SlimAdapterEx adapter;
    private SlimAdapterEx uiAdapter;

    public BreedingRecordPresenterImpl(Context context, BaseListView baseListView) {
        super(context, baseListView, new BreedingRecordModelImpl(context));
    }

    @Override
    public void upDatas(String... param) {
    }

    @Override
    public void setUI(RecyclerView recyclerView, String... param) {
    }

    public void setDataUI(RecyclerView recyclerView) {
        uiAdapter = SlimAdapter.create(SlimAdapterEx.class)
                .enableDiff()
                .registerDefault(R.layout.breeding_record_ui_item, new SlimInjector<JSONObject>() {
                    @Override
                    public void onInject(final JSONObject data, IViewInjector injector) {
                        try {
                            injector.text(R.id.muzhu, data.getString("muZhuErHao"))
                                    .text(R.id.gongzhu1, data.getString("gongZhu1ErHao"))
                                    .text(R.id.gongzhu2, data.getString("gongZhu2ErHao"))
                                    .text(R.id.gongzhu3, data.getString("gongZhu3ErHao"))
                                    .text(R.id.type, data.getString("pzTypeStr"))
                                    .text(R.id.pzy,data.getString("pzy"))
                                    .text(R.id.id, (uiAdapter.getData().size()-uiAdapter.getData().indexOf(data))+"")
                                    .clicked(R.id.deleteImg, new View.OnClickListener() {
                                        @Override
                                        public void onClick(View view) {
                                            Toast.makeText(context, "delete", Toast.LENGTH_SHORT).show();
                                            try {
                                                DataBaseUtil.excute(context, "delete from BreedRecordTable where _id ='" + data.getString("_id") + "'");
                                                upDataUI();
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    });
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                })
                .enableEmptyView(context, R.layout.empty_layout)
                .attachTo(recyclerView);
    }

    public void upDataUI() {
        uiAdapter.updateData(((BreedingRecordModelImpl) baseListModel).getUIDatas()).notifyDataSetChanged();
    }

    public void upActionDatas(String... params) {
        JSONObject jsonObject = new JSONObject();
        JSONArray jsonArray = DataBaseUtil.GetData(context, "select muZhuID,pzType,gongZhu1ID,gongZhu2ID,gongZhu3ID,pzy,pzyID from BreedRecordTable where lineID = '"+SPUtils.getInstance().getString(BaseApplication.FLINEID)+"'");
        if (jsonArray.length() > 0) {
            try {
                jsonObject.put("userID", SPUtils.getInstance().getString(BaseApplication.FUSERID));
//                jsonObject.put("pzDate", BreedingRecordModelImpl.pzdate);
                jsonObject.put("entry", jsonArray);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            ((BreedingRecordModelImpl) baseListModel).syncUpData(jsonObject.toString(), new WebService.NetSuccessListener() {
                @Override
                public void Success(String s) throws JSONException {
                    Toast.makeText(context, s.toString(), Toast.LENGTH_SHORT).show();
                    if (s.contains("配种记录提交并审核成功")) {
                        //清空数据库
                        DataBaseUtil.excute(context,"delete from BreedRecordTable where lineID = '"+SPUtils.getInstance().getString(BaseApplication.FLINEID)+"'");
                        upDataUI();
                    }
                }
            }, new WebService.NetFaildListener() {
                @Override
                public void Faild(String str) {

                }
            });
        } else {
            Toast.makeText(context, "当前无记录待上传", Toast.LENGTH_SHORT).show();
        }
    }
}
